// packageStore/pages/purchase/useraccount/index.js
import {router} from '../../../../js/router';
import {
  api,
  util,
  dialog,
  forms
} from '../../../../js/util';
import {
  finance
} from '../../../../js/modules/store';
import authorize from '../../../../js/modules/authorize';
import {
  area
} from "../../../../js/modules/gloab";
import BasePage from '../../../../js/common/base-page'
var app = getApp();
BasePage({

  /**
   * 页面的初始数据
   */
  data: {
    isIphoneX:app.globalData.isIphoneX,
    mainColor:api.titleColor||"#e56b14",//主题色
    PageIndex:1,
    PageSize:10,
    hasMoreData:true,
    isLoadComplete: false, // 是否加载完数据
    keyword:'',
    accountList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  loadList(){
    var that = this;
    var data={
      status:1,
      PageIndex:that.data.PageIndex,
      PageSize:that.data.PageSize,
      keyword:that.data.keyword
    };
    finance.getAccountList(data,function(content){
      let hasMore = content.count > that.data.PageIndex * that.data.PageSize;
      var data = content.data;
      //当分页大于1时，累加
      if (that.data.PageIndex > 1) {
        data = that.data.accountList.concat(data);
      }
      that.setData({
        accountList:data,
        hasMoreData: hasMore,
        isLoadComplete: true
      })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.setData({
      pageIndex: 1,
      isLoadComplete: false,
    })
    this.loadList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) { 
      var index = this.data.PageIndex;
      this.data.PageIndex = index + 1;
      this.loadList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 关键词搜索
   */
  onSearchKeywordEvent: function(e) {
    this.setData({
      pageIndex: 1,
      keyword: e.detail,
      isLoadComplete: false
    })
    this.loadList();
  },
  // 添加账户
  bindAdd(){
    router.navigate({
      path:'addAccount'
    })
  },
  bindEdit(e){
    var id = e.currentTarget.dataset.id;
    router.navigate({
      path:'addAccount',
      params:{
        id:id
      }
    })
  },
  bindDel(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否删除？',
      success:function(res){
        if(res.confirm){
          finance.getAccountDel({id:id},function(content){
            that.data.pageIndex = 1;
            that.loadList();
            util.tips('删除账户成功');
          });
        }else{
          return;
        }
      }
    })
  }
})